<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
class ListsController extends Controller{
	private $model;
	private $goodsattr_array;
	public function __init(){
		//调用CommonController里面的构造函数
		$this->model = new \Home\Model\Lists;
	}
	
	public function index(){
		//用户数据
		$usermodel = new \Home\Model\User;
		if(isset($_SESSION['uid'])||isset($_SESSION['username'])){
			$userdata = $usermodel->where("username='{$_SESSION['username']}'")->find();
			View::with('userdata',$userdata);
		}
		//点击品牌时传来cid 然后查找数据 以$goodsdata命名
		if($Qcid=Q('get.cid',0,'intval')){
			//分类数据 
			$catemodel = new \Home\Model\Cate;
			//全部
			$catedata = $catemodel->where("cid=$Qcid")->get();
			if($catedata[0]['pid']!==0) {
				$catedatat = $catemodel->where("cid=".$catedata[0]['pid'])->get();
				View::with('catedatat',$catedatat);
			}
			View::with('catedata',$catedata);
	
			//货品数据调用
			$catemodel2 = new \Admin\Model\Cate;
			//如果是顶级分类 就需要获取他的子级
			if($catedata[0]['pid']==0){
				$catedata = $catemodel2->get();
				$soncid = $catemodel2->getSon($catedata,$Qcid);
				$goodsdata = array();
				foreach ($soncid as $value) {
					$goodsdata1 = $this->model->where("category_cid=$value")->get();
					$goodsdata = array_merge($goodsdata1,$goodsdata);
				}
			}else{
				$goodsdata = $this->model->where("category_cid=$Qcid")->get();
			}
			
			//因为数据库 goodsattr 的 value 是用逗号隔开的字符串表示的 所以以下和老师讲的略有不同
			//∨∨∨∨∨∨∨∨∨∨∨∨不同∨∨∨∨∨∨∨∨∨∨∨∨∨∨
			
			//选择规格属性的导航
			//获得顶级分类
			$topCate = Db::table('category')->where("pid=0")->get();
			View::with('topCate',$topCate);
			//1.根据当前的cid获得对应的商品的gid
			$gids = $this->cidGetGids($Qcid);
			//2.根据gid找到商品属性
			$goodsAttr = $this->gidGetAttr($gids);
			View::with('goodsAttr',$goodsAttr);
//			p($goodsAttr);
			//3.处理筛选参数param 0_0_.....
			$num = count($goodsAttr);
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
			View::with('param',$param);
			//4.通过param参数筛选商品gid
			$resultGids = $this->filter($param,$gids);
			$re = array();
			foreach ($resultGids as $value) {
				$re = array_merge($re,$this->model->where("gid=$value")->get());
			}
			$goodsdata = $re;
			//∧∧∧∧∧∧∧∧∧∧∧∧不同∧∧∧∧∧∧∧∧∧∧∧∧∧∧
		}

		//点击品牌时传来bid 然后查找数据 同样以$goodsdata命名
		if($Qbid=Q('get.bid',0,'intval')){
			$goodsdata = $this->model->where("brand_bid=$Qbid")->get();
			//把bid再传回去 为了获取品牌的名称 显示在左上角
			View::with('Qbid',$Qbid);
			$catedata = '';
			View::with('catedata',$catedata);
		}
		View::with('goodsdata',$goodsdata);
		
		
		//总条数---用count还要每种情况再写一遍 所以直接遍历获取数量
		if($goodsdata){
			foreach ($goodsdata as $k => $v) {
			$goodsdatanum = $k +1;
			}
		}else{
			$goodsdatanum = 0;
		}
		View::with('goodsdatanum',$goodsdatanum);
		
		//获得品牌数据 并分配
		$brandmodel = new \Admin\Model\Brand;
		$branddata = $brandmodel->get();
		View::with('branddata',$branddata);
       	View::make();
	}
	//∨∨∨∨∨∨∨∨∨∨∨∨不同∨∨∨∨∨∨∨∨∨∨∨∨∨∨
	private function filter($param,$cidGids){
		$gids = array();
		foreach ($param as $kk => $vv) {
			if($vv){
				//1.找到value，比如找到"红色"
				foreach ($this->goodsattr_array as $key => $value) {
					if($value['arraykey']==$vv) $gavalue=$value['gavalue'];
				}
				//2.通过“红色”找到对应的商品id
				foreach ($this->goodsattr_array as $key => $value) {
					if($gavalue==$value['gavalue']) $gids[$kk][]=$value['goods_gid'];
				}
			}
		}
		//如果点击了“不限”之外的属性
		if($gids){
			//取交集
			foreach ($gids as $k => $v) {
				$gidsTemp = array_intersect($gids[$k], $v);
			}
			//和当前分类对应的gid再次取交集
			return array_intersect($gidsTemp, $cidGids);
		}else{//如果是全部是“不限”的时候
			return $cidGids;
		}
	}
	
		
	//通过gid获得商品的属性
	private function gidGetAttr($gids){
		$data = Db::table('goodsattr')->where("goods_gid in(" . implode(',', $gids) . ")")->get();
		//数据字符串变为数组  去重
		$this->goodsattr_array = $this->toarray($data);
//		p($data);
		$data = $this->assoc_unique($this->goodsattr_array ,'gavalue');
		//1.把相同类型属性id的归结在一起
		$sameAttr = array();
		foreach ($data as $v) {
			$sameAttr[$v['typeattr_taid']][] = $v;
		}
		//2.组合数组
		$temp = array();
		foreach ($sameAttr as $taid => $v) {
			$temp[] = array(
				'name' => Db::table('typeattr')->where("taid={$taid}")->pluck('taname'),
				'value' => $v
			);
		}
		
		return $temp;
	}
	
	//∧∧∧∧∧∧∧∧∧∧∧∧不同∧∧∧∧∧∧∧∧∧∧∧∧∧∧
	
	
	//通过cid获得gid
	private function cidGetGids($cid){
		//1.通过当前的cid获得对应的所有的子集cid
		$cids = $this->getSonCids(Db::table('category')->get(),$cid);
		$cids[] = $cid;
		//2.通过子集cid查找对应的商品的gid
		$gids = Db::table('goods')->where("category_cid in(" . implode(',', $cids) . ")")->lists('gid');
		return $gids;
	}
	
	//递归获得指定cid的所有子集
	private function getSonCids($data,$cid){
		$temp = array();
		foreach ($data as $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->getSonCids($data, $v['cid']));
			}
		}
		return $temp;
	}
	
	
	//goodsattr数据变为数组
	private function toarray($data){
		$newdata = array();
		foreach ($data as $kd => $vd) {
			foreach (explode(',', $vd['gavalue']) as $kg => $vg) {
				$newdata[] = array(
					'gaid' => $vd['gaid'],
					'gavalue' => $vg,
					'goods_gid' => $vd['goods_gid'],
					'typeattr_taid' => $vd['typeattr_taid'],
				);
			}
		}
		//给新的数组一个序号 方便以后用
		foreach ($newdata as $key => $value) {
			$newdata[$key]['arraykey'] = $key+1;
		}
		return $newdata;
	}
	
	//数组去重
	private function assoc_unique($arr, $key){
	    $tmp_arr = array();
	    foreach($arr as $k => $v){
		    if(in_array($v[$key], $tmp_arr)){
		    	//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
		    	unset($arr[$k]);
		    }else {
		    	$tmp_arr[] = $v[$key];
		    }
	  	}
		sort($arr); //sort函数对数组进行排序
		return $arr;
	}
	
	
	
}
	